package com.yuhs.zixing.study.multithread.mashibing.senior.s001;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Function: 门栓
 *
 * @Author yuhongsheng
 * @Date: 2024-01-24 21:57:46
 */
@Slf4j
public class S001CountDownLatch001 {

    public static void main(String[] args) {
        useCountDownLatch();

    }

    private static void useCountDownLatch() {

        Thread[] threads = new Thread[100];

        CountDownLatch latch = new CountDownLatch(threads.length);

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                int result = 0;
                for (int j = 0; j < 10; j++) {
                    result += j;
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                latch.countDown();
            });
        }

        for (Thread thread : threads) {
            thread.start();
        }

        try {
            latch.await();
        } catch (Exception e) {
            log.atInfo().log("异常", e);
        }

        log.atInfo().log("结束");
    }


}
